//
//  BlendCoincidence.swift
//  WatermarkCamera
//
//  Created by AxcLogo on 4/7/2024.
//

import UIKit
import Foundation

class BlendCoincidence: GPUImageTwoInputFilter {
    // Lifecycle

    override init!(vertexShaderFrom vertexShaderString: String!, fragmentShaderFrom fragmentShaderString: String!) {
        super.init(vertexShaderFrom: vertexShaderString, fragmentShaderFrom: fragmentShaderString)
    }

    override init() {
        super.init(fragmentShaderFrom: Self.BlendFragmentShaderString)
    }

    // Internal

    static let BlendFragmentShaderString =

        """



        varying highp vec2 textureCoordinate;



        varying highp vec2 textureCoordinate2;




        uniform sampler2D inputImageTexture;



        uniform sampler2D inputImageTexture2;




        void main()



        {




            lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);



            lowp vec4 textureColor2 = texture2D(inputImageTexture2, textureCoordinate);




            lowp vec4 outputColor = textureColor;



            if (textureColor2.a >= 1.0) {



                outputColor = textureColor2;



            } else {



                lowp float fa = textureColor2.a;



                lowp float rfa = (1.0 - fa);



                lowp float result_a = fa + textureColor.a * (1.0 - fa);



                lowp float result_r = (textureColor2.r + textureColor.r * textureColor.a * rfa ) / result_a;



                lowp float result_g = (textureColor2.g + textureColor.g * textureColor.a * rfa ) / result_a;



                lowp float result_b = (textureColor2.b + textureColor.b * textureColor.a * rfa ) / result_a;



                outputColor = vec4(result_r, result_g, result_b, result_a);



            }




            gl_FragColor = outputColor;



        }



        """
}
